/**
 *@Author: QWP
 *@Description: 控制审核元素显示
 *@Params:
 *@Date 2024-10-24 14:46
 */

import { AuditParams } from "./../common/model/auditParams";
import { computed, h, ref, watch } from "vue";
import router from "@/router";
import { useRoute } from "vue-router";

interface IPageTypeParams {
  result?: boolean;
  key?: string;
  [key: string]: any;
}
/**
 *@Author: QWP
 *@Description: 根据页面类型判断结果
 *@Params: mathValue 要匹配的值     props: 额外参数
 *@Date 2024-10-24 15:13
 */
export const usePageTypeShow = (mathValue: any, props?: IPageTypeParams) => {
  const { params } = router.currentRoute.value;
  const { key, result } = props || { key: "pType", result: true };
  if (mathValue instanceof Array) {
    return mathValue.some((i) => i == params[key]) ? result : false;
  } else {
    return mathValue == params[key] ? result : false;
  }
};

interface ISearchParams {
  result?: boolean;
  noResult?: boolean;
  key?: string;
}
export const useSearchShow = (searchF: object | any, matchValue: any, props?: ISearchParams) => {
  const defaultParams: any = {
    key: "status",
    result: true,
    noResult: false,
  };

  const { key, result, noResult } = { ...defaultParams, ...(props || {}) };

  const fReulst = ref<boolean>(false);
  watch(
    searchF,
    (nval: any, oval: any) => {
      let tValue = nval[key];
      if (matchValue instanceof Array) {
        fReulst.value = matchValue.some((i) => i == tValue) ? result : noResult;
      } else {
        fReulst.value = matchValue == tValue ? result : noResult;
      }
    },
    { immediate: true }
  );
  return computed(() => fReulst.value);
};

// 正常情况下显示取回按钮
export const showRecall = (searchF: object) => {
  const route = useRoute();
  const fReulst = ref<boolean>(false);
  watch(
    searchF,
    (nval: any) => {
      fReulst.value = route.params.pType == "1" || (nval.model.status == "3" && route.params.pType == "2");
    },
    { immediate: true }
  );
  return computed(() => fReulst.value);
};

// 正常情况下显示审核和退回按钮
export const showAudit = (searchF: object) => {
  const route = useRoute();
  const fReulst = ref<boolean>(false);
  watch(
    searchF,
    (nval: any) => {
      fReulst.value = nval.model.status == "2" && route.params.pType == "2";
    },
    { immediate: true }
  );
  return computed(() => fReulst.value);
};
